<template>
  <div class="register-container">
    <div class="register-layout">
      <div class="left-panel">
        <h1>嗨<span class="wave">👋</span> 新朋友</h1>
        <p style="margin: 2px 0;">是老朋友吗？</p>
        <p>曲阑深处重相见，点击下方登录</p>
        <el-button class="sign-in-btn" @click="$router.push('/login')">登录</el-button>
      </div>
      <div class="right-panel">
        <h2>新建账户</h2>
        
        <div class="typewriter-container">
            <span class="typewriter-text">{{ currentText }}</span>
            <span class="cursor" :class="{ 'blink': !isTyping }">|</span>
          </div>

        <el-form :model="registerForm" :rules="rules" ref="registerFormRef" class="register-form">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="用户名" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="registerForm.password" type="password" placeholder="密码" :prefix-icon="Lock" show-password></el-input>
          </el-form-item>
          <el-form-item prop="realName">
            <el-input v-model="registerForm.realName" placeholder="真实姓名" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input v-model="registerForm.email" placeholder="邮箱" :prefix-icon="Message"></el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input v-model="registerForm.phone" placeholder="手机号" :prefix-icon="Phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleRegister" :loading="loading" class="register-button">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'
import request from '../utils/request'

const router = useRouter()
const registerFormRef = ref(null)
const loading = ref(false)

// 打字机效果变量
const registerTips = [
  '📝 新的旅程即将开始，填写信息加入我们吧！',
  '🎭 创建你的专属账号，开启社团精彩故事',
  '🔑 注册成为会员，解锁更多隐藏功能',
  '🌈 每一个新用户都是我们社团的新色彩',
  '🚀 填写信息，一键起飞，社团活动等你来',
  '👥 已有千名小伙伴加入，就差你了！',
  '🏆 成为第一个知道最新活动的人'
]
const currentText = ref('')
const currentTipIndex = ref(0)
const isTyping = ref(true)
let typingTimer = null

const typeText = () => {
  const currentTip = registerTips[currentTipIndex.value]
  const targetLength = isTyping.value ? currentTip.length : 0
  
  if (isTyping.value && currentText.value.length < targetLength) {
    // 正在输入中
    currentText.value = currentTip.substring(0, currentText.value.length + 1)
    typingTimer = setTimeout(typeText, 50)
  } else if (!isTyping.value && currentText.value.length > 0) {
    // 正在删除中
    currentText.value = currentText.value.substring(0, currentText.value.length - 1)
    typingTimer = setTimeout(typeText, 30)
  } else if (isTyping.value) {
    // 输入完成，等待一会后开始删除
    isTyping.value = false
    typingTimer = setTimeout(typeText, 2000)
  } else {
    // 删除完成，切换到下一条提示
    currentTipIndex.value = (currentTipIndex.value + 1) % registerTips.length
    isTyping.value = true
    typingTimer = setTimeout(typeText, 500)
  }
}

// 组件挂载时执行
onMounted(() => {
  // 启动打字机效果
  typeText()
})

// 组件卸载时清除定时器
onUnmounted(() => {
  if (typingTimer) {
    clearTimeout(typingTimer)
  }
})

const registerForm = reactive({
  username: '',
  password: '',
  realName: '',
  role: 0,
  email: '',
  phone: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  realName: [{ required: false, message: '请输入真实姓名', trigger: 'blur' }],
  email: [
    { required: false, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: false, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  try {
    await registerFormRef.value.validate()
    loading.value = true
    
    const res = await request({
      url: '/auth/register',
      method: 'post',
      data: registerForm
    })
    
    console.log('请求完成，服务器返回：', res)

    if (res.success) {
      ElMessage.success('注册成功')
      router.push('/login')
    }
  } catch (error) {
    ElMessage.error(error.message || '注册失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
:root {
  --primary-color: #409EFF;
  --background-gradient: #f5f7fa;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  overflow: auto;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: var(--background-gradient);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  margin:0;
  padding:0;
  border:0
}

.register-container {
  height: 100vh;
  width: 100vw;
  display: grid;
  place-items: center;
  background: #f6f5f7;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.register-layout {
  width: 100%;
  max-width: 970px;
  height: 600px;
  display: flex;
  border-radius: 24px;
  box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);
  overflow: hidden;
}

.left-panel {
  flex: 0.8;
  background: linear-gradient(to right, #effd47, #e58881);
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 50px 30px;
}

.left-panel h1 {
  font-size: 36px;
  margin-bottom: 20px;
}

.left-panel p {
  font-size: 16px;
  line-height: 1.5;
  margin-bottom: 30px;
}

.sign-in-btn {
  background: transparent;
  border: 1px solid white;
  color: white;
  padding: 12px 45px;
  font-size: 16px;
  border-radius: 20px;
  text-transform: uppercase;
  transition: transform 0.3s ease;
}

.sign-in-btn:hover {
  transform: scale(1.05);
  background: rgba(255, 255, 255, 0.1);
}

.right-panel {
  flex: 1.4;
  background: white;
  padding: 50px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.right-panel h2 {
  font-size: 32px;
  margin-bottom: 20px;
  color: #333;
}

.social-icons {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.social-icon {
  width: 40px;
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #333;
  transition: all 0.3s ease;
  animation: bounce 1s ease infinite;
}

.social-icon:hover {
  background: #f5f5f5;
  transform: translateY(-2px);
}

.subtitle {
  color: #666;
  margin-bottom: 30px;
}

.register-form {
  width: 100%;
  max-width: 800px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 30px;
}

.register-form :deep(.el-form-item:last-child) {
  grid-column: span 2;
  margin-bottom: 0;
}
.register-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  background: linear-gradient(to right, #4776E6, #8E54E9);
  border: none;
  border-radius: 20px;
  color: white;
  text-transform: uppercase;
  margin-top: 20px;
  transition: all 0.3s ease;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 14px rgba(71, 118, 230, 0.3);
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 表单元素动画 */
.register-form .el-form-item {
  transition: transform 0.3s ease;
}

.register-form .el-form-item:hover {
  transform: translateX(5px);
}

.social-icon {
  animation: bounce 1s ease infinite;
}

.wave {
  display: inline-block;
  animation: wave-animation 2.5s infinite;
  transform-origin: 70% 70%;
}

@keyframes wave-animation {
  0% { transform: rotate(0deg); }
  10% { transform: rotate(14deg); }
  20% { transform: rotate(-8deg); }
  30% { transform: rotate(14deg); }
  40% { transform: rotate(-4deg); }
  50% { transform: rotate(10deg); }
  60% { transform: rotate(0deg); }
  100% { transform: rotate(0deg); }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

:deep(.el-input__wrapper) {
  background: #f5f5f5;
  border-radius: 25px;
  box-shadow: none !important;
  padding: 10px 20px;
}

:deep(.el-input__wrapper.is-focus) {
  background: white;
  box-shadow: 0 0 0 1px #4776E6 inset !important;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-input__prefix) {
  color: #666;
}

/* 添加打字机效果样式 */
.typewriter-container {
  height: 40px;
  margin: 10px 0 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.typewriter-text {
  font-size: 16px;
  color: #333;
  white-space: nowrap;
  height: 20px;
  display: inline-block;
}

.cursor {
  display: inline-block;
  margin-left: 2px;
  font-weight: bold;
  color: #333;
}

.cursor.blink {
  animation: blink 0.7s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .register-layout {
    flex-direction: column;
    height: auto;
    max-width: 90%;
    margin: 20px auto;
    border-radius: 16px;
  }

  .left-panel {
    flex: none;
    padding: 30px;
    border-radius: 24px 24px 0 0;
  }

  .left-panel h1 {
    font-size: 28px;
    margin-bottom: 10px;
  }

  .left-panel p {
    font-size: 14px;
    margin-bottom: 15px;
  }

  .right-panel {
    flex: none;
    padding: 20px;
    border-radius: 0 0 24px 24px;
  }

  .right-panel h2 {
    font-size: 24px;
  }

  .register-form {
    grid-template-columns: 1fr;
    gap: 0;
  }

  .register-form :deep(.el-form-item:last-child) {
    grid-column: span 1;
  }

  .typewriter-container {
    height: 60px;
  }

  .typewriter-text {
    font-size: 14px;
    text-align: center;
  }

  .register-button {
    height: 40px;
    font-size: 14px;
  }

  :deep(.el-input__inner) {
    height: 36px;
  }
}

/* 小屏幕手机适配 */
@media screen and (max-width: 375px) {
  .register-layout {
    max-width: 95%;
  }

  .left-panel h1 {
    font-size: 24px;
  }

  .left-panel p {
    font-size: 12px;
  }

  .right-panel h2 {
    font-size: 20px;
  }

  .typewriter-text {
    font-size: 12px;
  }
}

/* 横屏适配 */
@media screen and (max-height: 500px) and (orientation: landscape) {
  .register-layout {
    height: 90vh;
    margin: 5vh auto;
  }

  .left-panel,
  .right-panel {
    padding: 20px;
  }

  .left-panel h1 {
    font-size: 24px;
    margin-bottom: 10px;
  }

  .left-panel p {
    margin-bottom: 10px;
  }

  .register-form {
    max-width: 100%;
  }
}
</style>